package site.lanmushan.framework.query.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import site.lanmushan.framework.authorization.CurrentUser;
import site.lanmushan.framework.authorization.CurrentUserUtil;
import site.lanmushan.framework.dto.QueryInfo;
import site.lanmushan.framework.mapper.wrapper.DefaultWrapper;
import site.lanmushan.framework.mapper.wrapper.QueryWrapper;
import site.lanmushan.framework.query.entity.BaseEntity;
import site.lanmushan.framework.query.entity.BizEntity;
import site.lanmushan.framework.query.mapper.QueryMapper;
import site.lanmushan.framework.query.util.PageUtil;
import site.lanmushan.framework.query.util.QueryInfoUtil;
import site.lanmushan.framework.util.reflection.ReflectionUtil;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 */
public abstract class BaseService<T extends BaseEntity, M> implements IBaseService<T> {
    @Autowired
    ApplicationContext applicationContext;
    private M queryMapper;
    private Object lock = new Object();

    public M getQueryMapper() {
        if (queryMapper == null) {
            synchronized (lock) {
                if (queryMapper == null) {
                    Class queryMapperClass = ReflectionUtil.getSuperClassGenricType(this.getClass(), 1);
                    queryMapper = (M) applicationContext.getBean(queryMapperClass);
                }
            }
        }
        return queryMapper;
    }

    @Override
    public List selectList(QueryInfo queryInfo) {
        return ((QueryMapper) getQueryMapper()).selectAllList(QueryInfoUtil.toQueryWrapper(queryInfo));
    }

    @Override
    public List selectAllList() {
        PageUtil.startPage(1, 2000);
        return ((QueryMapper) getQueryMapper()).selectAll();

    }

    @Override
    public T selectById(Long id) {
        return (T) ((QueryMapper) getQueryMapper()).selectByPrimaryKey(id);
    }

    @Override
    public void logicDeleteById(Long id) {
        if (id == null) {
            return;
        }
        ((QueryMapper) getQueryMapper()).logicDeleteByPrimaryKey(id);
    }


    @Override
    public List selectPageList(QueryInfo queryInfo) {
        PageUtil.startPage(queryInfo);
        return ((QueryMapper) getQueryMapper()).selectAllList(QueryInfoUtil.toQueryWrapper(queryInfo));
    }

    @Override
    public void logicDeleteByIds(List<Long> ids) {
        if (ids == null || ids.size() == 0) {
            return;
        }
        QueryWrapper queryWrapper = new DefaultWrapper();
        queryWrapper.in("id", ids.toArray());
        ((QueryMapper) getQueryMapper()).logicDeleteQueryWrapper(queryWrapper);
    }

    @Override
    public void deleteServiceByIds(List<Long> ids) {
        ((QueryMapper) getQueryMapper()).deleteByIdList(ids);
    }

    @Override
    public void insertService(T entityAo) {
        if (entityAo instanceof BizEntity) {
            LocalDateTime now = LocalDateTime.now();
            BizEntity bizEntity = (BizEntity) entityAo;
            bizEntity.setCreateTime(now);
            bizEntity.setUpdateTime(now);
            try {
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                bizEntity.setCreateUserAccount(currentUser.getAccount());
                bizEntity.setUpdateUserAccount(currentUser.getAccount());
            } catch (Exception e) {
                //忽略掉
            }
        }
        ((QueryMapper) getQueryMapper()).insertSelective(entityAo);
    }

    @Override
    public int insertServiceList(List<T> entityList) {
        if (entityList == null || entityList.size() == 0) {
            return 0;
        }
        entityList.stream().forEach(entityAo -> {
            if (entityAo instanceof BizEntity) {
                LocalDateTime now = LocalDateTime.now();
                BizEntity bizEntity = (BizEntity) entityAo;
                bizEntity.setCreateTime(now);
                bizEntity.setUpdateTime(now);
                try {
                    CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                    bizEntity.setCreateUserAccount(currentUser.getAccount());
                    bizEntity.setUpdateUserAccount(currentUser.getAccount());
                } catch (Exception e) {
                    //忽略掉
                }
            }
        });
        return ((QueryMapper) getQueryMapper()).insertList(entityList);
    }

    @Override
    public void updateService(T entityAo) {
        if (entityAo instanceof BizEntity) {
            BizEntity bizEntity = (BizEntity) entityAo;
            bizEntity.setUpdateTime(LocalDateTime.now());
            try {
                CurrentUser currentUser = CurrentUserUtil.getCurrentUser();
                bizEntity.setUpdateUserAccount(currentUser.getAccount());
            } catch (Exception e) {
                //忽略掉
            }
        }
        ((QueryMapper) getQueryMapper()).updateByPrimaryKeySelective(entityAo);
    }


    public static void main(String[] args) {
        System.out.println(Integer.valueOf(1).compareTo(1));
    }
}
